package com.example.combwyaq.widget;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import androidx.annotation.Nullable;

import com.blankj.utilcode.util.ConvertUtils;
import com.example.combwyaq.R;

public class TimeLineView  extends View {
    private Bitmap selBit;//已经完成签到任务图片
    private Bitmap popBit;//翻倍气泡背景图
    private int selColor;//已完成任务
    private int normalColor;//默认任务颜色
    private String[] names={"1连签","2连签","3连签","4连签","5连签","6连签","7连签"};
    private String[] numes={"5","10","10","20","25","25","50"};
    private int width=0;//空间的宽度
    private int height=0;//控件的高度
    private int selIndex=0;//当前签到进度
    private int itemWidth=0;//每哥圈或线的宽度
    private int popHeight=0;//弹窗高度
    private int tvHeight= ConvertUtils.dp2px(20);//嵌套文字高度

    private Paint paint=new Paint(Paint.ANTI_ALIAS_FLAG);

    public TimeLineView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        width=getMeasuredWidth();
        height = getMeasuredHeight();
        itemWidth=width/13;
    }

    private void init(){
        selBit= BitmapFactory.decodeResource(getResources(), R.drawable.res_7icon_362);
        popBit= BitmapFactory.decodeResource(getResources(), R.drawable.module_integral_double_bg);
        selColor= Color.RED;
        normalColor=Color.GRAY;
        popHeight=popBit.getHeight();
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawSign(canvas);
    }

    private void drawSign(Canvas canvas){

        for (int i = 0; i <13 ; i++) {
            if(i%2==0){
                paint.setColor(normalColor);
                if(i<=selIndex*2){
                    paint.setColor(selColor);
                }
                canvas.drawCircle(itemWidth*i+itemWidth/2,
                        popHeight+(height-popHeight-tvHeight)/2-ConvertUtils.dp2px(25),itemWidth/2,paint);
                paint.setColor(Color.WHITE);
                canvas.drawCircle(itemWidth*i+itemWidth/2,
                        popHeight+(height-popHeight-tvHeight)/2-ConvertUtils.dp2px(25),
                        itemWidth/2,paint);
                /*绘制圆圈中间的文字签到积分*/
                paint.setColor(normalColor);
                if (i==6||i==12){
                    paint.setColor(selColor);
                }
                paint.setTextSize(itemWidth/2);
                paint.setFakeBoldText(true);
                paint.setTextAlign(Paint.Align.CENTER);
                canvas.drawText(numes[i/2],itemWidth*i+itemWidth/2,
                        popHeight+(height-popHeight-tvHeight)/2+(itemWidth/4)-ConvertUtils.dp2px(25),paint);
                paint.setColor(normalColor);
                /*绘制气泡弹窗*/
                if (i==6||i==12){
                    Rect rect = new Rect();
                    rect.left = itemWidth*i+ConvertUtils.dp2px(1);
                    rect.right = itemWidth*i+popBit.getWidth()-ConvertUtils.dp2px(50);
                    rect.top = 0;
                    rect.bottom = popHeight-popHeight/2;
                    canvas.drawBitmap(popBit,null,rect,paint);
                    paint.setTextSize(itemWidth/3);
                    paint.setColor(Color.WHITE);
                    canvas.drawText("翻倍",itemWidth*i+itemWidth-ConvertUtils.dp2px(15),
                            popHeight/2-ConvertUtils.dp2px(8),paint);
                    /*设置连签文字颜色*/
                    paint.setColor(selColor);

                }
                paint.setTextSize(itemWidth/3);
                canvas.drawText(names[i/2],itemWidth*i+itemWidth/2,height-ConvertUtils.dp2px(30),paint);

                /*绘制用户已签到图片*/
                if(i<=selIndex*2){
                    Rect dct=new Rect();
                    dct.left=itemWidth*i+ConvertUtils.dp2px(1);
                    dct.right=itemWidth*(i+1)-ConvertUtils.dp2px(1);
                    dct.top=popHeight+(height-popHeight-tvHeight)/2-itemWidth/2-ConvertUtils.dp2px(25);
                    dct.bottom=popHeight+(height-popHeight-tvHeight)/2+itemWidth/2-ConvertUtils.dp2px(25);
                    canvas.drawBitmap(selBit,null,dct,null);
                }
            }else{
                paint.setColor(normalColor);
                canvas.drawLine(itemWidth*i,
                        popHeight+(height-popHeight-tvHeight)/2-ConvertUtils.dp2px(1)/3-ConvertUtils.dp2px(25),
                        itemWidth*(i+1),
                        popHeight+(height-popHeight-tvHeight)/2-ConvertUtils.dp2px(25),paint);
            }
        }
    }

    @Override
    protected void onDetachedFromWindow() {
        super.onDetachedFromWindow();
        if(selBit!=null){
            selBit.recycle();
        }
        if(popBit!=null){
            popBit.recycle();
        }
    }
}
